package com.zhss.security.demospringsecurity.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @ClassName MyAuthenticationFailureHandler
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/9/9 17:31
 * @Version 1.0
 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		response.setContentType("application/json;charset=utf-8");
		HashMap<String, Object> resp = new HashMap<>();
		resp.put("status", 500);
		resp.put("msg", "登陆失败" + exception.getMessage());
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(resp);
		response.getWriter().write(json);
	}
}
